//
//  XUISwitchControlViewController.m
//  nav
//
//  Created by wjxfhxy on 15-7-6.
//  Copyright (c) 2015年 wjxfhxy. All rights reserved.
//

#import "XUISwitchControlViewController.h"

#import "UIViewController+XUIViewConroller.h"

@interface XUISwitchControlViewController ()
{
    NSMutableArray *_viewControllers;
}
@end

@implementation XUISwitchControlViewController

- (void)dealloc
{
    [_viewControllers removeAllObjects];
    [_viewControllers release_mm];
    superdealloc_mm;
}

- (void)viewDidLoad
{
    
}

- (void)viewWillLayoutSubviews
{
    BOOL lazy = NO;
    if(_viewControllers == nil)
    {
        lazy = [self lazyCreateDefaultIndexOfViewControllerClass];
    }
    
    if(lazy == NO)
        [self createDefaultIndexOfViewControllerClass];
}

-(void)createDefaultIndexOfViewControllerClass
{
    if(_viewControllers == nil)
    {
        _viewControllers = [NSMutableArray new];
        
        if(_viewControllers.count == 0)
        {
            NSUInteger n = [self numberOfSwitchViewController];
            for(NSInteger i = 0;i < n;i++)
                [_viewControllers addObject:[NSNull null]];
            
            NSUInteger defaultIndex = [self defaultCreateIndexOfViewControllerClass];

            [self createViewControllerClassAtIndex:defaultIndex block:^(Class cls,XUIViewControllerSwitchParam* param)
            {
                UIViewController *viewController = [[[cls alloc] initWithViewControllerSwitchParam:param] autorelease_mm];
                viewController.subVclDelegate = self;
                
                [_viewControllers replaceObjectAtIndex:defaultIndex withObject:viewController];
                    
                [self.view addSubview:viewController.view];
                    
                [self updateViewControllerNavigationItem:viewController.navigationItem];
                    
                [viewController viewControllerTapAppear];
                    
                [self viewControllerDidExchangeZLevel];
            }];
        }
    }
}

- (void)clickedSwitchControlAtIndex:(NSUInteger)index
{
    if(_viewControllers.count <= 0)
        return;
    
    if(index >= [self numberOfSwitchViewController])
        return;
    
    UIViewController *viewController = _viewControllers[index];
    if([viewController isKindOfClass:[NSNull class]])
    {
        [[self topViewController] viewControllerTapDisappear];
        
        [self createViewControllerClassAtIndex:index block:^(Class cls,XUIViewControllerSwitchParam* param)
        {
            UIViewController *viewController = [[[cls alloc] initWithViewControllerSwitchParam:param] autorelease_mm];
            viewController.subVclDelegate = self;
            
            [_viewControllers replaceObjectAtIndex:index withObject:viewController];
            
            [self.view addSubview:viewController.view];
            
            [self updateViewControllerNavigationItem:viewController.navigationItem];
            
            [viewController viewControllerTapAppear];
            
            [self viewControllerDidExchangeZLevel];
        }];
    }
    else if([self topViewController] == viewController)
    {
        [viewController viewControllerTapAppeared];
    }
    else
    {
        [[self topViewController] viewControllerTapDisappear];
        
        [self.view bringSubviewToFront:viewController.view];
        
        [self updateViewControllerNavigationItem:viewController.navigationItem];
        
        [viewController viewControllerTapReAppear];
        
        [self viewControllerDidExchangeZLevel];
    }
}

- (void)createAllViewControllerClass
{
    if(_viewControllers == nil)
        _viewControllers = [NSMutableArray new];
    
    NSUInteger index = [self numberOfSwitchViewController];
    
    for(NSUInteger i = 0; i < index; i++)
    {
        [self createViewControllerClassAtIndex:i block:^(Class cls,XUIViewControllerSwitchParam* param)
        {
            UIViewController *viewController = [[[cls alloc] initWithViewControllerSwitchParam:param] autorelease_mm];
            viewController.subVclDelegate = self;
            
            [_viewControllers addObject:viewController];
            
            if(i == 0)
            {
                [self.view addSubview:viewController.view];
                
                [self updateViewControllerNavigationItem:viewController.navigationItem];
                
                [viewController viewControllerTapAppear];
                
                [self viewControllerDidExchangeZLevel];
            }
            else
            {
                UIViewController *vcl = (UIViewController *)_viewControllers[i - 1];
                [self.view insertSubview:viewController.view belowSubview:vcl.view];
            }
        }];
    }
}

- (NSUInteger)numberOfSwitchViewController
{
    return 0;
}

- (void)updateViewControllerNavigationItem:(UINavigationItem*)navigationItem
{
    
}

@end
